package com.xxcloud.service.note.impl;

import java.util.Date;
import java.util.function.Predicate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xxcloud.common.utils.DateTimeUtil;
import com.xxcloud.common.utils.DateTimeUtil8;
import com.xxcloud.dao.dto.xxcloud.Note;
import com.xxcloud.dao.dto.xxcloud.NoteExample;
import com.xxcloud.dao.mapper.xxcloud.NoteMapper;
import com.xxcloud.model.exception.note.NoteException;
import com.xxcloud.model.web.note.dto.NoteDTO;
import com.xxcloud.model.web.note.vo.NoteVO;
import com.xxcloud.service.note.INoteService;

/**
 * Created by chenbingwen on 19/10/29.
 */
@Service("noteService")
public class NoteServiceImpl extends AbstractNoteService implements INoteService {

    @Autowired
    private NoteMapper noteMapper;

    @Override
    public boolean upsertNote(NoteDTO dto) throws NoteException {
        checkNoteDTO(dto);
        long operatorId = dto.getOperatorId();
        Note note = convert2Note(dto);
        Long id = dto.getId();
        if (id != null && id > 0) {
            boolean ifNoteExists = predicateIfExists(count(id), (count) -> count > 0);
            if (!ifNoteExists) {
                throw new NoteException(NoteException.NOTE_NOT_EXISTS);
            }
            id = updateNote(operatorId, note);
        } else {
            id = insertNote(operatorId, note);
        }
        return id != null && id > 0;
    }

    @Override
    public NoteVO getNoteDetail(long noteId) {
        NoteVO noteVO = new NoteVO();
        NoteExample example = new NoteExample();
        example.createCriteria().andIdEqualTo(noteId);
        Note note = noteMapper.selectByPrimaryKey(noteId);
        if (note != null) {
            BeanUtils.copyProperties(note, noteVO);
            noteVO.setCreateTime(DateTimeUtil.date2Long(note.getCreateTime()));
            noteVO.setUpdateTime(DateTimeUtil.date2Long(note.getUpdateTime()));
        }
        return noteVO;
    }

    private Note getNoteById(long noteId) {
        return noteMapper.selectByPrimaryKey(noteId);
    }

    private Long insertNote(long operatorId, Note note) {
        Date now = DateTimeUtil8.getCurrentDate();
        note.setCreateTime(now);
        note.setUpdateTime(now);
        note.setCreateUserId(operatorId);
        note.setUpdateUserId(operatorId);
        noteMapper.insertSelective(note);
        return note.getId();
    }

    private Long updateNote(long operatorId, Note note) {
        Long id = note.getId();
        if (id != null && id > 0) {
            Date now = DateTimeUtil8.getCurrentDate();
            note.setUpdateTime(now);
            note.setUpdateUserId(operatorId);
            noteMapper.updateByPrimaryKeySelective(note);
        }
        return id;

    }

    private int count(long id) {
        NoteExample example = new NoteExample();
        example.createCriteria().andIdEqualTo(id);
        return noteMapper.countByExample(example);
    }

    private boolean predicateIfExists(int size, Predicate<Integer> pre) {
        return pre.test(size);
    }
}
